import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:通过Callable接口创建有返回结果的线程
 * Date: 2024-09-02
 * Time: 10:05
 */
public class CallableExample {
    public static void main(String[] args) {
        //定义一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        Callable<Integer> task = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 123;
            }
        };

        Future<Integer> future = executorService.submit(task);

        try {
            Integer i = future.get();
            System.out.println("result = "+i);
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

    }
}
